package uit.jee.lib.access.mapper;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import uit.jee.lib.dto.WriterDTO;

/**
 *
 * @author ngloc_it
 */
public class WriterMapper extends MapperDB{

    public WriterMapper() throws Exception {
        super();
    }

    private void InitWriterDTOFromRs(WriterDTO wr, ResultSet  rs) throws Exception{
        if((rs != null) && (wr != null)){
            wr.setBookId(rs.getString(WriterDTO.WRITER_BOOK_ID));
            wr.setContents(rs.getString(WriterDTO.WRITER_CONTENTS));
            wr.setDate(rs.getString(WriterDTO.WRITER_DATE));
            wr.setReaderId(rs.getString(WriterDTO.WRITER_READER_ID));
            wr.setTitle(rs.getString(WriterDTO.WRITER_TITLE));
            wr.setWriterId(rs.getString(WriterDTO.WRITER_ID));
        }
    }

    public boolean CheckWriterExited(String writerId) throws Exception {
        boolean isExisted = false;

        try {
            StringBuffer sql = new StringBuffer();
            sql.append("Select * from booklibrary.baiviet where ");
            sql.append(WriterDTO.WRITER_ID).append(" = '");
            sql.append(writerId).append("'");

            PreparedStatement stmt = getConnection().prepareStatement(sql.toString());
            ResultSet rs = stmt.executeQuery();
            if ((rs != null) && (rs.next())) {
                isExisted = true;
            }
        } catch (Exception ex) {
            throw ex;
        }

        return isExisted;
    }

    public ArrayList<WriterDTO> GetAllWriter() throws Exception {
        ArrayList<WriterDTO> allreaders = new ArrayList<WriterDTO>();
        try {
            StringBuffer sql = new StringBuffer();
            sql.append("select * from  booklibrary.baiviet");
            PreparedStatement stmt = getConnection().prepareStatement(sql.toString());
            ResultSet rs = stmt.executeQuery();
            while (rs != null && rs.next()) {
                WriterDTO wt = new WriterDTO();
                InitWriterDTOFromRs(wt, rs);
                allreaders.add(wt);
            }
        } catch (Exception ex) {
            throw ex;
        }
        return allreaders;
    }

    public ArrayList<WriterDTO> GetAllWriterByAuthorId(String authorId) throws Exception {
        ArrayList<WriterDTO> allreaders = new ArrayList<WriterDTO>();
        try {
            StringBuffer sql = new StringBuffer();

            sql.append("select * from  booklibrary.baiviet where ");
            sql.append(WriterDTO.WRITER_READER_ID).append("=");
            sql.append("'").append(authorId).append("'");

            PreparedStatement stmt = getConnection().prepareStatement(sql.toString());
            ResultSet rs = stmt.executeQuery();
            while (rs != null && rs.next()) {
                WriterDTO wt = new WriterDTO();
                InitWriterDTOFromRs(wt, rs);
                allreaders.add(wt);
            }
        } catch (Exception ex) {
            throw ex;
        }
        return allreaders;
    }

    public ArrayList<WriterDTO> GetAllWriterByAuthorName(String authorName) throws Exception {
        return null;
    }

    public ArrayList<WriterDTO> GetAllWriterOnBook(String bookId) throws Exception {
        ArrayList<WriterDTO> allreaders = new ArrayList<WriterDTO>();
        try {
            StringBuffer sql = new StringBuffer();

            sql.append("select * from  booklibrary.baiviet where ");
            sql.append(WriterDTO.WRITER_BOOK_ID).append("=");
            sql.append("'").append(bookId).append("'");

            PreparedStatement stmt = getConnection().prepareStatement(sql.toString());
            ResultSet rs = stmt.executeQuery();
            while (rs != null && rs.next()) {
                WriterDTO wt = new WriterDTO();
                InitWriterDTOFromRs(wt, rs);
                allreaders.add(wt);
            }
        } catch (Exception ex) {
            throw ex;
        }
        return allreaders;
    }

    public void Insert(WriterDTO wr) throws Exception {
        try {
            StringBuffer sql = new StringBuffer();
            sql.append("Insert into booklibrary.baiviet values( ");
            sql.append("'").append(wr.getWriterId()).append("', ");
            sql.append("'").append(wr.getReaderId()).append("', ");
            sql.append("'").append(wr.getBookId()).append("', ");
            sql.append("'").append(wr.getTitle()).append("', '");
            sql.append(wr.getContents()).append("')");
            PreparedStatement stmt = getConnection().prepareStatement(sql.toString());
            stmt.execute();
            stmt.close();
        } catch (Exception e) {
            throw e;
        }
    }

    public void Insert(WriterDTO[] wr) throws Exception {
    }

    public void Update(WriterDTO wr) throws Exception {
        try {
            StringBuffer sql = new StringBuffer();
            sql.append("Update booklibrary.baiviet Set ");
            sql.append(" MaBaiViet = '").append(wr.getWriterId()).append("', ");
            sql.append(" MaDocGia = '").append(wr.getReaderId()).append("', ");
            sql.append(" MaSach = '").append(wr.getBookId()).append("', ");
            sql.append(" TieuDe = ").append(wr.getTitle());
            sql.append(" NoiDung = '").append(wr.getContents()).append("'");

            PreparedStatement stmt = getConnection().prepareStatement(sql.toString());
            stmt.executeUpdate();
            stmt.close();
        } catch (Exception ex) {
            throw ex;
        }

    }

    public void Update(WriterDTO[] wr) throws Exception {
    }

    public void Delete(WriterDTO wr) throws Exception {
        try {
            StringBuffer sql = new StringBuffer();
            sql.append("delete * from booklibrary.baiviet where ");
            sql.append(" MaBaiViet = '").append(wr.getWriterId()).append("'");
            PreparedStatement stmt = getConnection().prepareStatement(sql.toString());
            stmt.execute();
            stmt.close();
        } catch (Exception ex) {
            throw ex;
        }
    }

    public void Delete(WriterDTO[] wr) throws Exception {
    }

    public void Delete(String writerId) throws Exception {

        try {
            StringBuffer sql = new StringBuffer();
            sql.append("delete * from booklibrary.baiviet where ");
            sql.append(" MaBaiViet = '").append(writerId).append("'");
            PreparedStatement stmt = getConnection().prepareStatement(sql.toString());
            stmt.execute();
            stmt.close();
        } catch (Exception ex) {
            throw ex;
        }


    }

    public void Delete(String[] writerIds) throws Exception {
    }
}
